<?php
function G($var=null, $val="@#null#!")
{
    static $G;
    if(empty($G)){
        $G = &$_GET;
    }
    $val!="@#null#!" && $G[$var] = $val;
    if(empty($var))
        return $G;
    if(!isset($G[$var]))
        return null;
    return $G[$var];
}

function P($var=null, $val="@#null#!")
{
    static $P;
    if(empty($P)){
        $P = &$_POST;
    }
    $val!="@#null#!" && $P[$var] = $val;
    if(empty($var))
        return $P;
    if(!isset($P[$var]))
        return null;
    return $P[$var];
}

function GP($var=null, $val="@#null#!")
{
	static $P;
	if(empty($arr)){
		$P = array_merge(G(), P());
	}
	$val!="@#null#!" && $P[$var] = $val;
    if(empty($var))
        return $P;
    if(!isset($P[$var]))
        return null;
    return $P[$var];
}

function PG($var=null, $val="@#null#!")
{
	return GP($var, $val);
}

function C($var=null, $val="@#null#!", $expire=0,
            $path='.', $domain="", $secure=false, $httponly=false)
{
    if($val!="@#null#!"){
        if(!$domain){
           $domain = dirname($_SERVER["SCRIPT_URI"]);
        }
        setcookie($var, $val, $expire, $path, $domain, $secure, $httponly);
    }
    if(empty($var))
        return $_COOKIE;
    if(!isset($_COOKIE[$var]))
        return null;
    return $_COOKIE[$var];
}

function showJson($data)
{
	echo json_encode($data);
}

function S($var=null, $val="@#null#!")
{
    if(!isset($_SESSION)){
        session_start();
    }
    $val!="@#null#!" && $_SESSION[$var] = $val;
    if(empty($var))
        return $_SESSION;
    if(!isset($_SESSION[$var]))
        return null;
    return $_SESSION[$var];
}

function url($action)
{
	!empty($action) And $action = "/" . $action;
    return APP_HOME . "/index.php" . $action;
}

function o($str)
{
    if(!empty($str)) echo $str;
}

function is_ajax()
{
	return key_exists("ajax", pg());
}

function redirect($url, $wait=0)
{
	header("location:$url");
}

function br($str)
{
    echo "[br] " , $str , "<br />";
}

function def($const, $val)
{
    if (!defined($const)) {
        define($const, $val);
    }
}

function __autoload($class)
{
    import($class);
}

function import($class)
{
    $classes = func_get_args();
    foreach($classes as $class){
        if (file_exists($file = LIN_PATH . "/driver/{$class}.class.php")) {
            include_once $file;
        }else if (file_exists($file = LIN_PATH . "/helper/{$class}.class.php")) {
            include_once $file;
        }else if (file_exists($file = APP_PATH . "/controller/{$class}.class.php")) {
            include_once $file;
        }else if (file_exists($file = APP_PATH . "/model/{$class}.class.php")) {
            include_once $file;
        }else if (file_exists($file = APP_PATH . "/helper/{$class}.class.php")) {
        	include_once $file;
        }
    }
}

function form_select($attr, $data, $valKey, $captureKey, $selected=null)
{
	$options = "";
	foreach($data as $key => $one){
		if(!empty($selected) && $selected==$one[$valKey]) $options .= "<option value='{$one[$valKey]}' selected='selected'>{$one[$captureKey]}</option>\r\n";
		else $options .= "<option value='{$one[$valKey]}'>{$one[$captureKey]}</option>\r\n";
	}
	return "<select $attr>$options</select>";
}

function config($configName)
{
	static $configs = array();
	if(!isset($configs[$configName])){
		$configs[$configName] = include APP_PATH . "/config/{$configName}.php";
	}
	return $configs[$configName];
}

function html_table($ths, $data)
{
	$head = "<tr>";
	foreach($ths as $th){
		$head .= "<td>$th</td>";
	}
	$head .= "</tr>\r\n";
	$body = "";
	foreach($data as $row){
		$body .= "<tr>";
		foreach($row as $key => $cell){
			if($key == "created" || $key == "updated"){
				$cell = date('Y-m-d H:i:s', $cell);
			}
			$body .= "<td>$cell</td>";
		}
		$body .= "</tr>\r\n";
	}
	return "<table>$head $body </table>";
}

function dump($var)
{
    echo "<pre style='border:1px solid #ddd'>";
    print_r($var);
    echo "</pre>";
}


/**
 * 异常处理函数
 * @param Exception $e
 */
function lin_exception_handler($e) {
    ob_clean();
	echo "<div>LINPHP Exception Reporting</dic>";
    echo "<div style='border:1px solid #ddd; padding: 10px;color:red'>",
            $e->getMessage(),
         "</div>\n";

    echo "<pre>";
    $trace = $e->getTrace();
    echo "<div> Debug Trace: <br />";
    foreach($trace as $i=>$t){
        echo "<div style='border:1px dashed #eee;padding-top:10px;'>";
		!isset($t['file']) && $t['file'] = '';
		!isset($t['line']) && $t['line'] = '';

        $t['file'] = str_replace("\\", "/", $t["file"]);
        if(isset($t['class'])) $t['class'] .= '->';
        else $t['class']='';
        echo "<font color='blue'>#", $i, ' ', $t['class'] ,$t['function'] . '()' , '</font><br />';
        echo "<font color='green'>&nbsp;&nbsp;&nbsp;File: {$t['file']} (line {$t['line']})</font><br />";
        echo "</div>";
    }
    echo "</div>";
}

